import "package:ct_pa_majordomo/pages/apartment/api.dart";
import "package:ct_pa_majordomo/widgets/normal_scaffold/index.dart";
import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart';
import "package:ct_pa_majordomo/config/theme_vars.dart";
import "package:ct_pa_majordomo/pages/widgets/menu_block.dart";
import "package:ct_pa_majordomo/widgets/button/index.dart";
import "package:flutter_easyloading/flutter_easyloading.dart";

class ReminderNotOpenDoor extends StatefulWidget {
  const ReminderNotOpenDoor({Key? key}) : super(key: key);

  @override
  State<ReminderNotOpenDoor> createState() => _ReminderNotOpenDoorState();
}

class _ReminderNotOpenDoorState extends State<ReminderNotOpenDoor> {
  // 是否开启未开门提醒
  bool _isEnabled = false;
  // 选中的天数选项，默认3天
  int _selectedDays = 3;
  Map _apartmentInfo = {};
  
  // 未开门提醒天数选项
  final List<Map<String, dynamic>> _reminderOptions = [
    {"label": "3天未开门提醒", "value": 3},
    {"label": "7天未开门提醒", "value": 7},
    {"label": "15天未开门提醒", "value": 15},
    {"label": "30天未开门提醒", "value": 30},
  ];


  void _getApartmentInfo() async {
    EasyLoading.show(status: "加载中...");
    Map res = await apartmentApi.getApartmentInfo();
    setState(() {
      _apartmentInfo = res["data"];
      _selectedDays = _apartmentInfo["doorUnlockedReminderDays"];
      _selectedDays == 0 ? _isEnabled = false : _isEnabled = true;
    });
    EasyLoading.dismiss();
  }

  // 设置未开门提醒时间
  void _doorUnlockedReminder(int days) async {
    await apartmentApi.doorUnlockedReminder(days);
    EasyLoading.showSuccess("保存成功");
    _getApartmentInfo();
  }

    // 保存设置
  void _saveSettings() {
    _doorUnlockedReminder(_selectedDays);
  }

  @override
  void initState() {
    _getApartmentInfo();
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return NormalScaffold(
      title: "未开门提醒",
      child: Container(
        padding: const EdgeInsets.all(ThemeVars.paddingPage),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.stretch,
          children: [
            // 开关控制
            MenuBlock(
              items: [
                MenuBlockItem(
                  text: "未开门提醒",
                  showArrow: false,
                  suffix: CupertinoSwitch(
                    value: _isEnabled,
                    activeColor: ThemeVars.colorPrimary,
                    onChanged: (value) => setState(() {
                      _isEnabled = value;
                      _selectedDays = 3;
                    }),
                  ),
                ),
              ],
            ),
            const Padding(
              padding: EdgeInsets.only(top: 12),
              child: Text("功能开启后，你将收到房间多日未开门提醒通知。", style: TextStyle(color: ThemeVars.colorTextTer),),
            ),
            // 单选列表，仅在开启时显示
            if (_isEnabled) ...[
              const SizedBox(height: 12),
              MenuBlock(
                title: const Text("提醒时间", style: TextStyle(fontSize: 16, fontWeight: FontWeight.w500)),
                items: _reminderOptions.map((option) =>
                  MenuBlockItem(
                    text: option["label"]!,
                    showArrow: false,
                    suffix: _selectedDays == option["value"]
                      ? const Icon(Icons.check_circle, color: ThemeVars.colorPrimary)
                      : const Icon(Icons.circle_outlined, color: ThemeVars.colorPrimary),
                    onTap: () => setState(() {
                      _selectedDays = option["value"]!;
                    }),
                  )
                ).toList(),
              ),
            ],
            
            // 保存按钮
            Padding(
              padding: const EdgeInsets.only(top: 60),
              child: Button(
                type: ButtonType.primary,
                onPressed: _saveSettings,
                child: const Text("保存"),
              ),
            ),
          ],
        ),
      ),
    );
  }
}

